<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Assignment Form</title>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f9f9f9;
            font-family: Arial, sans-serif;
        }

        .container {
            background: white;
            box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
            border-radius: 10px;
            padding: 20px;
        }

        h2 {
            color: #4caf50;
            border-bottom: 3px solid #4caf50;
            padding-bottom: 10px;
            margin-bottom: 30px;
        }

        label {
            font-weight: bold;
        }

        .form-control, textarea {
            border-radius: 5px;
            border: 1px solid #ced4da;
        }

        .form-check-label {
            margin-left: 10px;
        }

        .btn-primary {
            background-color: #4caf50;
            border: none;
        }

        .btn-primary:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <div class="container mt-5">
        <h2>布置作业</h2>
        <form id="setAssignment" enctype="multipart/form-data">
            <div class="form-group">
                <label for="assignmentTitle">作业标题</label>
                <input type="text" class="form-control" id="assignmentTitle" name="assignmentTitle" placeholder="请输入作业标题" required>
            </div>
            <div class="form-group">
                <label for="assignmentContent">作业内容</label>
                <textarea class="form-control" id="assignmentContent" name="assignmentContent" rows="5" placeholder="请输入作业内容" required></textarea>
            </div>
            <div class="form-group">
                <label for="file">上传文件</label>
                <input type="file" class="form-control" id="file" name="file">
            </div>

            <div class="form-group">
                <label for="weight">作业权重</label>
                <div id="weight">
                    {% for i in range(1, 11) %}
                    <div class="form-check">
                        <input
                            type="radio"
                            class="form-check-input"
                            id="weight_{{ i }}"
                            name="weight"
                            value="{{ i }}"
                            {% if weight == i %} checked {% endif %}>
                        <label class="form-check-label" for="weight_{{ i }}"> {{ i }} </label>
                    </div>
                    {% endfor %}
                </div>
            </div>

            <button type="submit" class="btn btn-primary btn-block">提交</button>
        </form>
    </div>

    <script>
        document.getElementById('setAssignment').addEventListener('submit', function(event) {
            event.preventDefault();

            const assignmentTitle = document.getElementById('assignmentTitle').value;
            const assignmentContent = document.getElementById('assignmentContent').value;
            const file = document.getElementById('file').files[0];
            const weightElement = document.querySelector('input[name="weight"]:checked');
            const weight = weightElement ? weightElement.value : '';

            const formData = new FormData();
            formData.append('assignmentTitle', assignmentTitle);
            formData.append('assignmentContent', assignmentContent);
            formData.append('file', file);
            formData.append('class_id', {{ class_id }});
            formData.append('weight', weight);

            fetch('/assignments/assignment/set_assignment', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.error) {
                    alert(data.error);
                } else if (data.message) {
                    alert('作业设置成功！');
                    window.location.href = '/classes/class/class_dashboard/{{ class_id }}';
                }
            })
            .catch(error => {
                alert(error.message);
            });
        });
    </script>
</body>
</html>
